using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using Spacejock;
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace yW5Lib
{
	public class clsScene : clsSceneBase
	{
		private clsRTF cRTF;

		private DateTime LastChangedRTF;

		private DateTime LastChangedMarkup;

		internal string mvarRTFNeverUseOutside;

		internal string mvarMarkupNeverUseOutside;

		private bool _0024STATIC_0024SaveRTFV5_00242012E_0024AlreadySaving;

		public override string TextRTF
		{
			get
			{
				if (V6)
				{
					ConvertToRTFIfNec();
				}
				return mvarRTFNeverUseOutside;
			}
			set
			{
				if (Operators.CompareString(mvarRTFNeverUseOutside, value, TextCompare: false) != 0)
				{
					mvarRTFNeverUseOutside = value;
					mvarMarkupNeverUseOutside = cRTF.RTFToMarkup(value);
					LastChangedRTF = DateAndTime.Now;
					LastChangedMarkup = LastChangedRTF;
					UpdateCount = true;
					RTFNeedsSaving = true;
				}
			}
		}

		public clsScene()
		{
			cRTF = new clsRTF(ref clsShared.cLog, ref clsShared.cSettings);
			mvarRTFNeverUseOutside = "";
			mvarMarkupNeverUseOutside = "";
		}

		public override void Clear()
		{
			base.Clear();
			mvarRTFNeverUseOutside = "";
			mvarMarkupNeverUseOutside = "";
		}

		internal override void ConvertToPlainIfNec()
		{
			if (DateTime.Compare(LastChangedRTF, LastConvertedToPlain) != 0)
			{
				mvarPlainText = ConvertToPlain(TextRTF);
				ContainsYW2Code = mvarPlainText.Contains("<@@@>");
				LastConvertedToPlain = LastChangedRTF;
			}
		}

		private void ConvertToMarkupIfNec()
		{
			if (DateTime.Compare(LastChangedRTF, LastChangedMarkup) != 0)
			{
				ForceConvertToMarkup();
			}
		}

		private void ConvertToRTFIfNec()
		{
			if (DateTime.Compare(LastChangedRTF, LastChangedMarkup) != 0)
			{
				ForceConvertToRTF();
			}
		}

		internal override string MarkedUpTextV6Only()
		{
			return mvarMarkupNeverUseOutside;
		}

		public override bool LoadRTFV5(string Projectpath)
		{
			bool result = false;
			try
			{
				if (Operators.CompareString(mvarRTFFile, "", TextCompare: false) == 0)
				{
					throw new Exception("No RTF filename in scene " + Title);
				}
				string text = Path.Combine(modSubs.RTFPathV5(Projectpath), mvarRTFFile);
				if (!FileExists(text))
				{
					OverrideWordcount(0);
					clsShared.cLog.AddAction("Scene file " + text + " is missing.");
					if (Interaction.MsgBox("Scene file " + text + " is missing. Do you want to continue loading the project?\r\n(This is only recommended if you know the scene was blank, or contained nothing really important)", MsgBoxStyle.YesNo | MsgBoxStyle.Critical) == MsgBoxResult.No)
					{
						Interaction.MsgBox("Project load cancelled.");
						throw new Exception("Scene file " + text + " is missing. Project load cancelled");
					}
					if (!WriteTextFile(text, "", UseSysEncoding: false))
					{
						Interaction.MsgBox("Unable to create new, empty scene file " + text);
						throw new Exception("Unable to create new, empty scene file " + text);
					}
				}
				string Content = "";
				if (!LoadTextFile(text, ref Content, UseSysEncoding: false))
				{
					throw new Exception("An error occurred while trying to load " + text);
				}
				result = true;
				LastChangedRTF = DateAndTime.Now;
				MarkUpdated(FileDate(text));
				ContainsYW2Code = Content.Contains("<@@@>");
				TextRTF = Content;
				if (mvarWordCount != 0)
				{
					return result;
				}
				DoCount();
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public override bool SaveRTFV5(string ProjectPath)
		{
			bool result = true;
			if (!mvarV6)
			{
				if (!_0024STATIC_0024SaveRTFV5_00242012E_0024AlreadySaving)
				{
					_0024STATIC_0024SaveRTFV5_00242012E_0024AlreadySaving = true;
					try
					{
						if (RTFNeedsSaving | (Operators.CompareString(mvarRTFFile, "", TextCompare: false) == 0))
						{
							if (Operators.CompareString(mvarRTFFile, "", TextCompare: false) == 0)
							{
								mvarRTFFile = GenerateRTFFilename(ProjectPath);
							}
							string text = Path.Combine(modSubs.RTFPathV5(ProjectPath), mvarRTFFile);
							clsShared.cLog.AddAction("Writing scene " + Conversions.ToString(mvarID) + " to " + text);
							if (SaveTextFile(text, TextRTF, UseSysEncoding: false))
							{
								RTFNeedsSaving = false;
							}
							else
							{
								clsShared.cLog.AddAction("Could not save " + text);
								if (Operators.CompareString(ConvertedToPlainText().Trim(), "", TextCompare: false) == 0)
								{
									clsShared.cLog.AddAction("Not a critical problem - the scene has no content");
								}
								else
								{
									result = false;
									RTFNeedsSaving = true;
								}
							}
						}
					}
					catch (Exception ex)
					{
						ProjectData.SetProjectError(ex);
						Exception ex2 = ex;
						clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
						ProjectData.ClearProjectError();
					}
					_0024STATIC_0024SaveRTFV5_00242012E_0024AlreadySaving = false;
					try
					{
						if (!clsShared.cSettings.sGet("CreateTextToo", DefaultIfUnknown: false))
						{
							return result;
						}
						string expression = clsShared.cFile.FileWithoutExt(mvarRTFFile) + ".txt";
						expression = Strings.Replace(expression, "RTF_", "Plain_");
						expression = Path.Combine(Path.Combine(ProjectPath, "Data"), expression);
						MkMissingdir(Path.Combine(ProjectPath, "Data"));
						if (WritePlainText(expression, ConvertedToPlainText()))
						{
							return result;
						}
						clsShared.cLog.AddAction("Could not save plain text to " + expression);
						return result;
					}
					catch (Exception ex3)
					{
						ProjectData.SetProjectError(ex3);
						Exception ex4 = ex3;
						clsShared.cLog.AddAction(ex4, MethodBase.GetCurrentMethod());
						ProjectData.ClearProjectError();
						return result;
					}
				}
				clsShared.cLog.AddAction("Already saving", MethodBase.GetCurrentMethod());
			}
			return result;
		}

		public bool EraseSceneFile(string Filename)
		{
			bool result = true;
			try
			{
				if (Operators.CompareString(Filename, "", TextCompare: false) == 0)
				{
					return result;
				}
				clsShared.cFile.EraseFile(Filename);
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				result = false;
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool EraseSceneFileV5()
		{
			return EraseSceneFile(base.RTFFilename);
		}

		public bool SaveTextFile(string Filename, string txtRTF, bool UseSysEncoding)
		{
			return clsShared.cFile.SaveTextFile(Filename, txtRTF, UseSysEncoding);
		}

		public bool WritePlainText(string Filename, string Content)
		{
			return clsShared.cFile.WriteTextFile(Filename, Content);
		}

		public bool WriteTextFile(string Filename, string Content, bool UseSysEncoding)
		{
			return clsShared.cFile.WriteTextFile(Filename, Content);
		}

		public bool LoadTextFile(string Filename, ref string Content, bool UseSysEncoding)
		{
			return clsShared.cFile.LoadTextFile(Filename, ref Content);
		}

		public void MkMissingdir(string Folder)
		{
			clsShared.cFile.MkMissingDir(Folder);
		}

		public DateTime FileDate(string Filename)
		{
			return clsShared.cFile.FileDate(Filename);
		}

		public bool FileExists(string Filename)
		{
			return clsShared.cFile.FileExists(Filename);
		}

		public bool FolderExists(string Foldername)
		{
			return clsShared.cFile.FolderExists(Foldername);
		}

		public string GenerateRTFFilename(string ProjectPath)
		{
			Collection AllFiles = new Collection();
			string result = "";
			try
			{
				string text = modSubs.RTFPathV5(ProjectPath);
				clsShared.cFile.FillFiles(text, "*.RTF", ref AllFiles);
				new clsCollections(ref clsShared.cLog).SortSimpleCollection_DeletesKeys(ref AllFiles);
				int num = (AllFiles.Count != 0) ? ((int)Math.Round(Conversion.Val(Strings.Replace(Conversions.ToString(AllFiles[AllFiles.Count]), "RTF_", "", 1, -1, CompareMethod.Text)))) : 0;
				num++;
				bool flag;
				string text2;
				do
				{
					flag = false;
					text2 = "RTF_" + Strings.Format(num, "00000") + ".rtf";
					if (clsShared.cFile.FileExists(Path.Combine(text, text2)))
					{
						flag = true;
						num++;
					}
				}
				while (flag);
				result = text2;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(Information.Err(), MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public string ConvertToPlain(string strRTF)
		{
			RichTextBox richTextBox = new RichTextBox();
			try
			{
				richTextBox.Rtf = strRTF;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				richTextBox.Text = strRTF;
				ProjectData.ClearProjectError();
			}
			return richTextBox.Text;
		}

		public void ForceConvertToMarkup()
		{
			try
			{
				mvarMarkupNeverUseOutside = cRTF.RTFToMarkup(mvarRTFNeverUseOutside);
				LastChangedMarkup = LastChangedRTF;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				ProjectData.ClearProjectError();
			}
		}

		public void ForceConvertToRTF()
		{
			try
			{
				mvarRTFNeverUseOutside = cRTF.MarkupToRTF(mvarMarkupNeverUseOutside);
				LastChangedRTF = DateAndTime.Now;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				ProjectData.ClearProjectError();
			}
		}

		public override void SetMarkup(string strMarkup)
		{
			try
			{
				mvarMarkupNeverUseOutside = strMarkup;
				mvarRTFNeverUseOutside = cRTF.MarkupToRTF(mvarMarkupNeverUseOutside);
				LastChangedRTF = DateAndTime.Now;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				ProjectData.ClearProjectError();
			}
		}
	}
}
